<?php
	include_once("libs/util.php");
	
	function loadPicture($file,$mime="jpg"){
		$image= imagecreatefromjpeg("../avbase/".$file);		
		// Date in the past 
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
		
		// always modified 
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
		
		// HTTP/1.1 
		header("Cache-Control: no-store, no-cache, must-revalidate"); 
		header("Cache-Control: post-check=0, pre-check=0", false); 
		
		// HTTP/1.0 
		header("Pragma: no-cache"); 	
		
		if ($mime=="jpg"){
			// send the content type header so the image is displayed properly
			header('Content-type: image/jpeg');
		}else{
			header('Accept-Ranges: bytes');
			header('Content-Transfer-Encoding: Binary');
			header('Content-Type: application/force-download');
			header('Content-Disposition: attachment; filename="'.$file.'"');
		}
		
		// send the image to the browser
		imagejpeg($image);
		
		// destroy the image to free up the memory
		imagedestroy($image);		
		
		if ($mime=="binary"){
		    $conn = mysql_connect('localhost', 'avatnet_admin', 'themasculinestate') or die ('Error, MySQL database connections has failed.');
		    mysql_select_db('avatnet_avatar24');

        	$cdata = mysql_query("SELECT download FROM  pics WHERE file='$file'") or die(mysql_error()); 
        	$rd = mysql_fetch_array( $cdata );
        	$download=$rd["download"];
        
        	$download=$download+1;
        
			$result = mysql_query( "UPDATE pics SET download=$download WHERE file='$file'") or die( mysql_error());			
		}
	}
	
	if ($ssVar=="[pic]"){ 
		loadPicture($var["pic"]); 
		}
		
	if ($ssVar=="[get]"){ 
		loadPicture($var["get"],"binary"); 
		}
?>